package com.company.bookstore.security;

import com.company.bookstore.common.constant.RequestConstant;
import com.company.bookstore.common.redis.RedisUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.logout.LogoutFilter;

import javax.annotation.Resource;

/**
 * @author 吴经纶
 * 循环依赖解决
 * https://www.icode9.com/content-1-552748.html
 * @date 2020-08-27 15:34
 * @blame Android Team
 */

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

      @Resource
      UsernamePasswordProvider usernamePasswordProvider;
      @Resource
      AdminnamePasswordProvider adminnamePasswordProvider;
      @Resource
      RedisUtil redisUtil;


      @Override
      protected void configure(AuthenticationManagerBuilder auth) {
            auth.authenticationProvider(usernamePasswordProvider).authenticationProvider(adminnamePasswordProvider);
      }


      @Override
      public AuthenticationManager authenticationManager () throws Exception {
          return super.authenticationManager();
      }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()

                .authorizeRequests()
                .antMatchers(RequestConstant.HTTP_LOGIN_URI).permitAll()
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/v2/**").permitAll()
                .antMatchers("/swagger-resources/**").permitAll() //放行丝袜哥
                .antMatchers("/*/register").permitAll()  //放行注册
                .antMatchers("/book/**").permitAll()          //给book模块放行
                .antMatchers("/public/**").permitAll()          //给book模块放行
                .antMatchers("/category/**").permitAll()          //给category模块放行
                //cart放行
                .antMatchers("/api/cart/**").permitAll()
                //monitor放行
                .antMatchers("/api/monitor/**").permitAll()
                //data放行
                .antMatchers("/api/data/**").permitAll()
                //export放行
                .antMatchers("/api/export/**").permitAll()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
//                .antMatchers(HttpMethod.POST).permitAll()
//                .antMatchers(HttpMethod.PUT).permitAll()
                .antMatchers(HttpMethod.DELETE).hasAnyAuthority("ROLE_NONE","ROLE_ADMIN")
                .antMatchers(HttpMethod.PUT).hasAnyAuthority("ROLE_NONE","ROLE_ADMIN")

//                .antMatchers(HttpMethod.DELETE).permitAll()

                .anyRequest()
//                .permitAll()
                .authenticated()
                .and()
                .addFilterAfter(new UsernamePasswordFilter(authenticationManager(),redisUtil), LogoutFilter.class)
                .addFilterAfter(new JwtFilter(redisUtil), UsernamePasswordFilter.class)
                .cors().disable()
                .csrf().disable()   ;
    }
//https://blog.csdn.net/qq_36582604/article/details/80500245

//    @Bean
//    public FilterRegistrationBean<CorsFilter> corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        CorsConfiguration config = new CorsConfiguration();
//        config.setAllowCredentials(true);	config.addAllowedOrigin("http://localhost:9527");
//        config.addAllowedOrigin("null");
//        config.addAllowedHeader("*");
//        config.addAllowedMethod("*");
//        source.registerCorsConfiguration("/**", config); // CORS 配置对所有接口都有效
//        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
//        bean.setOrder(0);
//        return bean;
//    }

}
